import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GroundItems;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Tiles;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSObject.Type;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(name="NGHunter", authors={"Rakura"}, keywords={"Hunter"}, version=1.0D, description="crim swifts, trop wags, <grey,red> chins")
public class NGHunter extends Script
{
  private NGHunter.Constants consts;
  private NGHunter.HunterTile originalTile;
  private int nTraps;
  private final ArrayList<NGHunter.HunterTile> myTiles = new ArrayList();
  private boolean waiting;
  private int phailCount;

  public boolean onStart()
  {
    if (!this.walking.isRunEnabled()) {
      this.walking.setRun(true);
    }
    int i = this.skills.getCurrentLevel(21);
    this.consts = new NGHunter.Constants(i);

    RSTile localRSTile = getMyPlayer().getLocation();
    this.originalTile = new NGHunter.HunterTile(this, localRSTile.getX(), localRSTile.getY());

    if (i < 20) {
      this.nTraps = 1;
      this.myTiles.add(this.originalTile);
    } else if (i < 40) {
      this.nTraps = 2;

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() + 1, this.originalTile.getY() + 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() - 1, this.originalTile.getY() + 1));
    }
    else if (i < 60) {
      this.nTraps = 3;

      this.myTiles.add(this.originalTile);

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() + 1, this.originalTile.getY() + 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() - 1, this.originalTile.getY() + 1));
    }
    else if (i < 80) {
      this.nTraps = 4;

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() + 1, this.originalTile.getY() + 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() - 1, this.originalTile.getY() + 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() - 1, this.originalTile.getY() - 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() + 1, this.originalTile.getY() - 1));
    }
    else {
      this.nTraps = 5;

      this.myTiles.add(this.originalTile);

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() + 1, this.originalTile.getY() + 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() - 1, this.originalTile.getY() + 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() - 1, this.originalTile.getY() - 1));

      this.myTiles.add(new NGHunter.HunterTile(this, this.originalTile.getX() + 1, this.originalTile.getY() - 1));
    }

    return true;
  }

  private boolean needCleaning()
  {
    return this.inventory.getCount() >= 26;
  }

  private void cleanInventory() {
    RSItem[] arrayOfRSItem = this.inventory.getItems(new int[] { 9978, 526 });

    ArrayList localArrayList = new ArrayList(Arrays.asList(arrayOfRSItem));
    Collections.shuffle(localArrayList);
    for (RSItem localRSItem : localArrayList)
      if (localRSItem.getID() == 9978)
        localRSItem.doAction("Drop");
      else if (localRSItem.getID() == 526)
        if (this.consts.bury)
          localRSItem.doClick(true);
        else
          localRSItem.doAction("Drop");
  }

  public int loop()
  {
    if ((this.consts.level < 53) && (
      (this.waiting) || (needCleaning()))) {
      cleanInventory();
    }
    return loopAction();
  }

  private int loopAction() {
    this.mouse.setSpeed(random(5, 8));
    verifyTiles();
    NGHunter.HunterTile localHunterTile = nextTile(null);
    if (localHunterTile != null)
    {
      boolean bool;
      switch (NGHunter.1.$SwitchMap$NGHunter$TileType[localHunterTile.type.ordinal()]) {
      case 1:
        bool = reset(localHunterTile);
        break;
      case 2:
      case 3:
        bool = collect(localHunterTile);
        if (!bool) break;
        bool = setup(localHunterTile); break;
      case 4:
        if (this.inventory.containsOneOf(new int[] { this.consts.trap })) {
          bool = setup(localHunterTile);
        } else {
          this.myTiles.remove(localHunterTile);
          this.nTraps -= 1;
          bool = true;
        }
        break;
      default:
        bool = false;
      }
      if (bool)
        this.phailCount = 0;
      else {
        this.phailCount += 1;
      }

    }

    if (this.phailCount > 10)
      stopScript();
    return 0;
  }

  private void verifyTiles() {
    NGHunter.HunterTile[] arrayOfHunterTile = (NGHunter.HunterTile[])this.myTiles.toArray(new NGHunter.HunterTile[this.myTiles.size()]);
    for (int i = 0; i < arrayOfHunterTile.length; i++) {
      NGHunter.HunterTile localHunterTile1 = arrayOfHunterTile[i];
      if ((!localHunterTile1.isOurTrap) && (!isFree(localHunterTile1))) {
        NGHunter.HunterTile localHunterTile2 = getOptimalFreeTile(4);
        if (localHunterTile2 != null) {
          this.myTiles.set(i, localHunterTile2);
        } else {
          this.myTiles.remove(i);
          this.nTraps -= 1;
        }
      }
    }
  }

  private NGHunter.HunterTile nextTile(NGHunter.HunterTile paramHunterTile)
  {
    int i = 0;
    Collections.sort(this.myTiles);
    for (Iterator localIterator = this.myTiles.iterator(); localIterator.hasNext(); ) { localHunterTile = (NGHunter.HunterTile)localIterator.next();
      localObject1 = this.groundItems.getAllAt(localHunterTile);
      for (localObject3 : localObject1)
        if (localObject3.getItem().getID() == this.consts.trap) {
          this.waiting = false;
          localHunterTile.type = NGHunter.TileType.FALLEN;
          localHunterTile.associated = localObject3;
          if (localHunterTile != paramHunterTile)
            return localHunterTile;
        }
    }
    NGHunter.HunterTile localHunterTile;
    Object localObject1;
    Object localObject3;
    for (localIterator = this.myTiles.iterator(); localIterator.hasNext(); ) { localHunterTile = (NGHunter.HunterTile)localIterator.next();
      localObject1 = this.objects.getAt(localHunterTile, 1);
      for (localObject3 : localObject1) {
        int m = localObject3.getID();
        if (m == this.consts.caught) {
          this.waiting = false;
          localHunterTile.type = NGHunter.TileType.CAUGHT;
          localHunterTile.associated = localObject3;
          if (localHunterTile != paramHunterTile)
            return localHunterTile;
        }
        if (m == this.consts.failed) {
          this.waiting = false;
          localHunterTile.type = NGHunter.TileType.FAILED;
          localHunterTile.associated = localObject3;
          if (localHunterTile != paramHunterTile)
            return localHunterTile;
        }
        if (m == this.consts.wait) {
          i++;
          if (i == this.nTraps) {
            this.waiting = true;
            return null;
          }
        }
      }
    }
    for (localIterator = this.myTiles.iterator(); localIterator.hasNext(); ) { localHunterTile = (NGHunter.HunterTile)localIterator.next();
      if (isFree(localHunterTile)) {
        this.waiting = false;
        localHunterTile.type = NGHunter.TileType.EMPTY;
        localHunterTile.associated = null;
        if (localHunterTile != paramHunterTile)
          return localHunterTile;
      }
    }
    return (NGHunter.HunterTile)null;
  }

  private NGHunter.HunterTile getOptimalFreeTile(int paramInt) {
    int i = this.originalTile.getX() - paramInt;
    int j = this.originalTile.getX() + paramInt;
    int k = this.originalTile.getY() - paramInt;
    int m = this.originalTile.getY() + paramInt;
    ArrayList localArrayList = new ArrayList();
    for (int n = i; n <= j; n++) {
      for (int i1 = k; i1 <= m; i1++) {
        NGHunter.HunterTile localHunterTile = new NGHunter.HunterTile(this, n, i1);
        if (isFree(localHunterTile))
          localArrayList.add(localHunterTile);
      }
    }
    return getNearest(this.originalTile, localArrayList);
  }

  private boolean isFree(NGHunter.HunterTile paramHunterTile) {
    for (RSObject localRSObject : this.objects.getAllAt(paramHunterTile)) {
      RSObject.Type localType = localRSObject.getType();
      if ((localType == RSObject.Type.INTERACTABLE) || (localType == RSObject.Type.BOUNDARY))
      {
        return false;
      }
    }
    return true;
  }

  private NGHunter.HunterTile getNearest(NGHunter.HunterTile paramHunterTile, List<NGHunter.HunterTile> paramList) {
    Object localObject = null;
    double d1 = 1.7976931348623157E+308D;
    for (NGHunter.HunterTile localHunterTile : paramList) {
      double d2 = this.calc.distanceBetween(paramHunterTile, localHunterTile);
      if (d2 < d1) {
        localObject = localHunterTile;
        d1 = d2;
      }
    }
    return localObject;
  }

  private boolean setup(NGHunter.HunterTile paramHunterTile) {
    if ((walk(paramHunterTile)) && 
      (clickInventoryItem(this.consts.trap))) {
      return trapUp(paramHunterTile);
    }

    return false;
  }

  private boolean reset(NGHunter.HunterTile paramHunterTile) {
    RSGroundItem localRSGroundItem = (RSGroundItem)paramHunterTile.associated;
    if (localRSGroundItem.doAction("Lay")) {
      return trapUp(paramHunterTile);
    }
    return false;
  }

  private boolean trapUp(NGHunter.HunterTile paramHunterTile) {
    if (waitForAnim(3000)) {
      NGHunter.HunterTile localHunterTile = nextTile(paramHunterTile);
      if (localHunterTile != null)
        hoverTile(localHunterTile);
      while (!getMyPlayer().isIdle()) {
        sleep(100);
      }

      if (localHunterTile != null)
        hoverTile(localHunterTile);
      if (trapIsHere(paramHunterTile)) {
        paramHunterTile.isOurTrap = true;
        paramHunterTile.type = NGHunter.TileType.UP;
        return true;
      }
    }
    return false;
  }

  private boolean collect(NGHunter.HunterTile paramHunterTile) {
    RSObject localRSObject = (RSObject)paramHunterTile.associated;
    localRSObject.doClick();
    if (waitForAnim(3000)) {
      while ((trapIsHere(paramHunterTile)) && 
        (!getMyPlayer().isIdle()))
      {
        sleep(100);
      }
      if (!trapIsHere(paramHunterTile)) {
        paramHunterTile.isOurTrap = false;
        paramHunterTile.type = NGHunter.TileType.EMPTY;
        return true;
      }
    }
    return false;
  }

  private void hoverTile(RSTile paramRSTile) {
    if (!paramRSTile.equals(this.tiles.getTileUnderMouse())) {
      Point localPoint = this.calc.tileToScreen(paramRSTile);
      this.mouse.move(localPoint, 5, 5);
    }
  }

  private void atTile(RSTile paramRSTile) {
    hoverTile(paramRSTile);
    this.mouse.click(true);
  }

  private boolean trapIsHere(RSTile paramRSTile) {
    RSObject[] arrayOfRSObject1 = this.objects.getAt(paramRSTile, 1);
    for (RSObject localRSObject : arrayOfRSObject1) {
      int k = localRSObject.getID();
      if ((k == this.consts.wait) || (k == this.consts.caught) || (k == this.consts.failed)) {
        return true;
      }
    }
    return false;
  }

  private boolean walk(RSTile paramRSTile) {
    atTile(paramRSTile);
    waitForMove(3000);
    while ((!onTile(paramRSTile)) && 
      (!getMyPlayer().isIdle()))
    {
      moveToInventoryItem(this.consts.trap);
      sleep(100);
    }
    return onTile(paramRSTile);
  }

  private boolean onTile(RSTile paramRSTile) {
    return (getMyPlayer().getLocation().equals(paramRSTile)) && (getMyPlayer().isIdle());
  }

  private boolean clickInventoryItem(int paramInt)
  {
    if (moveToInventoryItem(paramInt)) {
      this.mouse.click(true);
      return true;
    }
    return false;
  }

  private boolean moveToInventoryItem(int paramInt) {
    if (this.game.getCurrentTab() != 4)
      this.game.openTab(4);
    RSItem localRSItem = this.inventory.getItem(new int[] { paramInt });
    RSComponent localRSComponent = localRSItem.getComponent();
    if (localRSComponent == null)
      return false;
    localRSComponent.doHover();
    return true;
  }

  private boolean waitForAnim(int paramInt) {
    long l = System.currentTimeMillis();
    while (System.currentTimeMillis() - l < paramInt) {
      if (getMyPlayer().getAnimation() != -1)
        return true;
    }
    return false;
  }

  private boolean waitForMove(int paramInt) {
    long l = System.currentTimeMillis();
    while (System.currentTimeMillis() - l < paramInt) {
      if (getMyPlayer().isMoving())
        return true;
    }
    return false;
  }
}